package com.dingup.mock.bean.exam;


import com.dingup.mock.bean.listening.ListeningAnswerInfo;
import com.dingup.mock.bean.listening.ListeningSummaryInfo;
import com.dingup.mock.bean.reading.ReadingAnswerInfo;
import com.dingup.mock.bean.reading.ReadingSummaryInfo;
import com.dingup.mock.bean.speaking.SpeakingAnswerInfo;
import com.dingup.mock.bean.writing.WritingAnswerInfo;
import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by yanggavin on 16/5/7.
 */
public class ExamSummaryInfo {
    private List<ListeningSummaryInfo> listeningSummaryInfos;
    private List<ReadingSummaryInfo> readingSummaryInfos;
    private List<SpeakingAnswerInfo> speakingAnswerInfos;
    private List<WritingAnswerInfo> writingAnswerInfos;
    private AnswerResultInfo readingAnswerResult;
    private AnswerResultInfo listeningAnswerResult;

    public ExamSummaryInfo(){
        listeningSummaryInfos = new ArrayList<>();
        readingSummaryInfos = new ArrayList<>();
        speakingAnswerInfos = new ArrayList<>();
        writingAnswerInfos = new ArrayList<>();
        readingAnswerResult = new AnswerResultInfo();
        listeningAnswerResult = new AnswerResultInfo();
    }

    /**
     * 计算作答结果统计信息:阅读和听力两部分
     */
    public void calAnswerResultInfo(){
        if (readingSummaryInfos.size() > 0){
            for (ReadingSummaryInfo rsi : readingSummaryInfos){
                for (ReadingAnswerInfo rai : rsi.getAnswerInfoList()){
                    setResultData(readingAnswerResult, rai.getStudentAnswer(), rai.getCorrectAnswer());
                }
            }
        }
        if (listeningSummaryInfos.size() > 0){
            for (ListeningSummaryInfo lsi : listeningSummaryInfos){
                for (ListeningAnswerInfo lai : lsi.getAnswerInfoList()){
                    setResultData(listeningAnswerResult, lai.getStudentAnswer(), lai.getCorrectAnswer());
                }
            }
        }
    }

    /**
     * 根据作答结果统计具体数目
     * @param ari
     * @param sa
     * @param ca
     */
    private void setResultData(AnswerResultInfo ari, String sa, String ca){
        if (StringUtils.isBlank(sa)){
            ari.setPass(ari.getPass() + 1);
        } else {
            if (sa.equalsIgnoreCase(ca)){
                ari.setCorrect(ari.getCorrect() + 1);
            } else {
                ari.setWrong(ari.getWrong() + 1);
            }
        }
        ari.setTotal(ari.getTotal() + 1);
    }

    public List<ListeningSummaryInfo> getListeningSummaryInfos() {
        return listeningSummaryInfos;
    }

    public void setListeningSummaryInfos(List<ListeningSummaryInfo> listeningSummaryInfos) {
        this.listeningSummaryInfos = listeningSummaryInfos;
    }

    public List<ReadingSummaryInfo> getReadingSummaryInfos() {
        return readingSummaryInfos;
    }

    public void setReadingSummaryInfos(List<ReadingSummaryInfo> readingSummaryInfos) {
        this.readingSummaryInfos = readingSummaryInfos;
    }

    public List<SpeakingAnswerInfo> getSpeakingAnswerInfos() {
        return speakingAnswerInfos;
    }

    public void setSpeakingAnswerInfos(List<SpeakingAnswerInfo> speakingAnswerInfos) {
        this.speakingAnswerInfos = speakingAnswerInfos;
    }

    public List<WritingAnswerInfo> getWritingAnswerInfos() {
        return writingAnswerInfos;
    }

    public void setWritingAnswerInfos(List<WritingAnswerInfo> writingAnswerInfos) {
        this.writingAnswerInfos = writingAnswerInfos;
    }

    public AnswerResultInfo getReadingAnswerResult() {
        return readingAnswerResult;
    }

    public AnswerResultInfo getListeningAnswerResult() {
        return listeningAnswerResult;
    }
}
